/*   1:    */ package com.atomikos.finitestates;
/*   2:    */ 
/*   3:    */ import java.util.Enumeration;
/*   4:    */ import java.util.Hashtable;
/*   5:    */ 
/*   6:    */ public class FSMImp
/*   7:    */   implements FSM
/*   8:    */ {
/*   9: 48 */   private Object state_ = null;
/*  10: 51 */   private Hashtable enterlisteners_ = null;
/*  11: 54 */   private Hashtable preenterlisteners_ = null;
/*  12: 57 */   private Hashtable transitionlisteners_ = null;
/*  13: 60 */   private Hashtable pretransitionlisteners_ = null;
/*  14: 63 */   private TransitionTable transitiontable_ = null;
/*  15: 66 */   private Object eventsource_ = null;
/*  16:    */   private Object stateLatch_;
/*  17:    */   
/*  18:    */   public FSMImp(TransitionTable transitiontable, Object initialstate)
/*  19:    */   {
/*  20: 82 */     this(null, transitiontable, initialstate);
/*  21: 83 */     this.eventsource_ = this;
/*  22:    */   }
/*  23:    */   
/*  24:    */   public FSMImp(Object eventsource, TransitionTable transitiontable, Object initialstate)
/*  25:    */   {
/*  26: 99 */     this.transitiontable_ = transitiontable;
/*  27:100 */     this.state_ = initialstate;
/*  28:101 */     this.enterlisteners_ = new Hashtable();
/*  29:102 */     this.preenterlisteners_ = new Hashtable();
/*  30:103 */     this.transitionlisteners_ = new Hashtable();
/*  31:104 */     this.pretransitionlisteners_ = new Hashtable();
/*  32:105 */     this.eventsource_ = eventsource;
/*  33:106 */     this.stateLatch_ = new Object();
/*  34:    */   }
/*  35:    */   
/*  36:    */   protected synchronized void addEnterListener(Hashtable listeners, Object lstnr, Object state)
/*  37:    */   {
/*  38:121 */     Hashtable lstnrs = (Hashtable)listeners.get(state);
/*  39:122 */     if (lstnrs == null) {
/*  40:123 */       lstnrs = new Hashtable();
/*  41:    */     }
/*  42:124 */     if (!lstnrs.containsKey(lstnr)) {
/*  43:125 */       lstnrs.put(lstnr, new Object());
/*  44:    */     }
/*  45:126 */     listeners.put(state, lstnrs);
/*  46:    */   }
/*  47:    */   
/*  48:    */   protected synchronized void addTransitionListener(Hashtable listeners, Object lstnr, Object from, Object to)
/*  49:    */   {
/*  50:143 */     Hashtable lstnrs = (Hashtable)listeners.get(from);
/*  51:144 */     if (lstnrs == null) {
/*  52:145 */       lstnrs = new Hashtable();
/*  53:    */     }
/*  54:146 */     Hashtable tolstnrs = (Hashtable)lstnrs.get(to);
/*  55:147 */     if (tolstnrs == null) {
/*  56:148 */       tolstnrs = new Hashtable();
/*  57:    */     }
/*  58:149 */     if (!tolstnrs.containsKey(lstnr)) {
/*  59:150 */       tolstnrs.put(lstnr, new Object());
/*  60:    */     }
/*  61:151 */     lstnrs.put(to, tolstnrs);
/*  62:152 */     listeners.put(from, lstnrs);
/*  63:    */   }
/*  64:    */   
/*  65:    */   protected void notifyListeners(Hashtable listeners, Object state, boolean pre)
/*  66:    */   {
/*  67:166 */     Hashtable lstnrs = null;
/*  68:167 */     FSMEnterEvent event = new FSMEnterEvent(this.eventsource_, state);
/*  69:168 */     synchronized (this)
/*  70:    */     {
/*  71:170 */       lstnrs = (Hashtable)listeners.get(state);
/*  72:171 */       if (lstnrs == null) {
/*  73:172 */         return;
/*  74:    */       }
/*  75:175 */       lstnrs = (Hashtable)lstnrs.clone();
/*  76:    */     }
/*  77:179 */     Enumeration enumm = lstnrs.keys();
/*  78:180 */     while (enumm.hasMoreElements())
/*  79:    */     {
/*  80:181 */       Object listener = enumm.nextElement();
/*  81:182 */       if ((pre) && ((listener instanceof FSMPreEnterListener))) {
/*  82:183 */         ((FSMPreEnterListener)listener).preEnter(event);
/*  83:184 */       } else if ((!pre) && ((listener instanceof FSMEnterListener))) {
/*  84:185 */         ((FSMEnterListener)listener).entered(event);
/*  85:    */       }
/*  86:    */     }
/*  87:    */   }
/*  88:    */   
/*  89:    */   protected void notifyListeners(Hashtable listeners, Object from, Object to, boolean pre)
/*  90:    */   {
/*  91:201 */     FSMTransitionEvent event = new FSMTransitionEvent(this.eventsource_, from, to);
/*  92:202 */     Hashtable lstnrs = null;
/*  93:203 */     Hashtable tolstnrs = null;
/*  94:204 */     synchronized (this)
/*  95:    */     {
/*  96:205 */       lstnrs = (Hashtable)listeners.get(from);
/*  97:206 */       if (lstnrs == null) {
/*  98:207 */         return;
/*  99:    */       }
/* 100:208 */       tolstnrs = (Hashtable)lstnrs.get(to);
/* 101:209 */       if (tolstnrs == null) {
/* 102:210 */         return;
/* 103:    */       }
/* 104:214 */       lstnrs = (Hashtable)lstnrs.clone();
/* 105:215 */       tolstnrs = (Hashtable)tolstnrs.clone();
/* 106:    */     }
/* 107:219 */     Enumeration enumm = tolstnrs.keys();
/* 108:220 */     while (enumm.hasMoreElements())
/* 109:    */     {
/* 110:221 */       Object listener = enumm.nextElement();
/* 111:222 */       if ((pre) && ((listener instanceof FSMPreTransitionListener))) {
/* 112:223 */         ((FSMPreTransitionListener)listener).beforeTransition(event);
/* 113:225 */       } else if ((!pre) && ((listener instanceof FSMTransitionListener))) {
/* 114:226 */         ((FSMTransitionListener)listener).transitionPerformed(event);
/* 115:    */       }
/* 116:    */     }
/* 117:    */   }
/* 118:    */   
/* 119:    */   public Object getState()
/* 120:    */   {
/* 121:240 */     Object ret = null;
/* 122:242 */     synchronized (this.stateLatch_)
/* 123:    */     {
/* 124:243 */       ret = this.state_;
/* 125:    */     }
/* 126:245 */     return ret;
/* 127:    */   }
/* 128:    */   
/* 129:    */   private void setStateObject(Object state)
/* 130:    */   {
/* 131:252 */     synchronized (this.stateLatch_)
/* 132:    */     {
/* 133:253 */       this.state_ = state;
/* 134:    */     }
/* 135:    */   }
/* 136:    */   
/* 137:    */   public void setState(Object state)
/* 138:    */     throws IllegalStateException
/* 139:    */   {
/* 140:265 */     Object oldstate = null;
/* 141:266 */     synchronized (this)
/* 142:    */     {
/* 143:267 */       if (!this.transitiontable_.legalTransition(this.state_, state)) {
/* 144:268 */         throw new IllegalStateException("Transition not allowed: " + this.state_ + " to " + state);
/* 145:    */       }
/* 146:270 */       oldstate = this.state_;
/* 147:271 */       notifyListeners(this.preenterlisteners_, state, true);
/* 148:272 */       notifyListeners(this.pretransitionlisteners_, oldstate, state, true);
/* 149:273 */       setStateObject(state);
/* 150:    */     }
/* 151:276 */     notifyListeners(this.enterlisteners_, state, false);
/* 152:277 */     notifyListeners(this.transitionlisteners_, oldstate, state, false);
/* 153:    */   }
/* 154:    */   
/* 155:    */   public void addFSMEnterListener(FSMEnterListener lstnr, Object state)
/* 156:    */   {
/* 157:287 */     addEnterListener(this.enterlisteners_, lstnr, state);
/* 158:    */   }
/* 159:    */   
/* 160:    */   public void addFSMPreEnterListener(FSMPreEnterListener lstnr, Object state)
/* 161:    */   {
/* 162:299 */     addEnterListener(this.preenterlisteners_, lstnr, state);
/* 163:    */   }
/* 164:    */   
/* 165:    */   public void addFSMTransitionListener(FSMTransitionListener lstnr, Object from, Object to)
/* 166:    */   {
/* 167:310 */     addTransitionListener(this.transitionlisteners_, lstnr, from, to);
/* 168:    */   }
/* 169:    */   
/* 170:    */   public void addFSMPreTransitionListener(FSMPreTransitionListener lstnr, Object from, Object to)
/* 171:    */   {
/* 172:320 */     addTransitionListener(this.pretransitionlisteners_, lstnr, from, to);
/* 173:    */   }
/* 174:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\atomikos-util\3.7.0\atomikos-util-3.7.0.jar
 * Qualified Name:     com.atomikos.finitestates.FSMImp
 * JD-Core Version:    0.7.0.1
 */